﻿using System;
using System.Collections.Generic;
using System.Text;
using Cashier_DAO.db;
using Cashier_Model;
using Cashier_DAO;
using System.Data;
using Cashier_Utils;
using System.Data.Common;

namespace Cashier_Service
{
    public class GoodsService
    {
        /// <summary>
        /// 查询商品信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="barcode"></param>
        /// <returns></returns>
        public Goods GetGoods(long id, String barcode)
        {
            Goods goods = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    goods = new Goods();
                    goods.Id = id;
                    goods.Barcode = barcode;
                    goods = new GoodsDAO().GetGoods(conn, goods);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return goods;
        }

        /// <summary>
        /// 查询商品信息列表
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="barcode"></param>
        /// <param name="name"></param>
        /// <param name="categoryid"></param>
        /// <param name="discount"></param>
        /// <param name="isvipprice"></param>
        /// <param name="stock"></param>
        /// <param name="ascfield"></param>
        /// <param name="descfield"></param>
        /// <returns></returns>
        public DataTable GetGoodsTable(int pageindex, int pagesize, String barcode, String name, long categoryid, decimal discount, Boolean? isvipprice,
            int stock, String ascfield, String descfield)
        {
            DataTable dt = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    Goods goods = new Goods();
                    goods.Pageindex = pageindex;
                    goods.Pagesize = pagesize;
                    goods.Barcode = barcode;
                    goods.Name = name;
                    goods.Categoryid = categoryid;
                    goods.Discount = discount;
                    goods.Isvipprice = isvipprice;
                    goods.Stock = stock;
                    goods.Ascfield = ascfield;
                    goods.Descfield = descfield;
                    dt = new GoodsDAO().GetGoodsTable(conn, goods);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        /// <summary>
        /// 查询商品信息列表
        /// </summary>
        /// <param name="barcode"></param>
        /// <param name="name"></param>
        /// <param name="categoryid"></param>
        /// <param name="discount"></param>
        /// <param name="isvipprice"></param>
        /// <param name="stock"></param>
        /// <param name="suppliername"></param>
        /// <param name="warehousename"></param>
        /// <returns></returns>
        public long GetGoodsRecord(String barcode, String name, long categoryid, decimal discount, Boolean? isvipprice, int stock)
        {
            long record = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    Goods goods = new Goods();
                    goods.Barcode = barcode;
                    goods.Name = name;
                    goods.Categoryid = categoryid;
                    goods.Discount = discount;
                    goods.Isvipprice = isvipprice;
                    goods.Stock = stock;
                    record = new GoodsDAO().GetGoodsRecord(conn, goods);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return record;
        }

        /// <summary>
        /// 插入商品信息
        /// </summary>
        /// <param name="goods"></param>
        /// <returns></returns>
        public long InsertGoods(Goods goods)
        {
            long id = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    id = new GoodsDAO().InsertGoods(conn, goods);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return id;
        }

        /// <summary>
        /// 更新商品信息
        /// </summary>
        /// <param name="goods"></param>
        /// <returns></returns>
        public int UpdateGoods(Goods goods)
        {
            int flag = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    flag = new GoodsDAO().UpdateGoods(conn, goods);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        /// <summary>
        /// 更新商品库存
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="id">商品ID</param>
        /// <param name="numbers">更新数量</param>
        /// <param name="type">类型：增加或减少</param>
        /// <returns></returns>
        public int UpdateGoodsStock(DbConnection conn, long id, int numbers, String type)
        {
            int flag = 0;
            try
            {
                Goods goods = new Goods();
                goods.Id = id;
                goods = new GoodsDAO().GetGoods(conn, goods);
                if (goods != null)
                {
                    if (type == ActKey.ADD)
                        goods.Stock = goods.Stock + numbers;
                    else if (type == ActKey.SUB)
                    {
                        goods.Stock = goods.Stock - numbers;
                        if (goods.Stock < 0)
                            goods.Stock = 0;
                    }
                    flag = new GoodsDAO().UpdateGoodsStock(conn, goods);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        /// <summary>
        /// 更新商品进货成本价
        /// </summary>
        /// <param name="id"></param>
        /// <param name="priceCost"></param>
        /// <returns></returns>
        public int UpdateGoodsPriceCost(long id, decimal priceCost)
        {
            int flag = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    Goods goods = new Goods();
                    goods.Id = id;
                    goods.Pricecost = priceCost;
                    flag = new GoodsDAO().UpdateGoodsSingleField(conn, goods);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        /// <summary>
        /// 更新商品销售价
        /// </summary>
        /// <param name="id"></param>
        /// <param name="priceSale"></param>
        /// <returns></returns>
        public int UpdateGoodsPriceSale(long id, decimal priceSale)
        {
            int flag = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    Goods goods = new Goods();
                    goods.Id = id;
                    goods.Pricesale = priceSale;
                    flag = new GoodsDAO().UpdateGoodsSingleField(conn, goods);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        /// <summary>
        /// 更新商品折扣
        /// </summary>
        /// <param name="id"></param>
        /// <param name="discount"></param>
        /// <returns></returns>
        public int UpdateGoodsDiscount(long id, Decimal discount)
        {
            int flag = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    Goods goods = new Goods();
                    goods.Id = id;
                    goods.Discount = discount;
                    flag = new GoodsDAO().UpdateGoodsSingleField(conn, goods);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        /// <summary>
        /// 更新会员价资格
        /// </summary>
        /// <param name="id"></param>
        /// <param name="isvipprice"></param>
        /// <returns></returns>
        public int UpdateIsvipprice(long id, bool isvipprice)
        {
            int flag = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    Goods goods = new Goods();
                    goods.Id = id;
                    goods.Isvipprice = isvipprice;
                    flag = new GoodsDAO().UpdateGoodsSingleField(conn, goods);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        /// <summary>
        /// 删除商品信息
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int DeleteGoods(String ids)
        {
            int count = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    if (!String.IsNullOrEmpty(ids))
                    {
                        conn.Open();
                        String[] arr = ids.Split(new Char[] { ',' });
                        GoodsDAO goodsDAO = new GoodsDAO();
                        Goods goods = new Goods();
                        foreach (String lopid in arr)
                        {
                            goods.Id = Int64.Parse(lopid);
                            count += goodsDAO.DeleteGoods(conn, goods);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return count;
        }

        /// <summary>
        /// 查询商品各分类数量
        /// </summary>
        /// <returns></returns>
        public DataTable GetCategoryGroupTable()
        {
            DataTable dt = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    dt = new GoodsDAO().GetCategoryGroupTable(conn, null);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }
    }
}
